#!/bin/bash

# IMChat 项目构建脚本
# 支持Linux和macOS

set -e  # 遇到错误时退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 显示帮助信息
show_help() {
    echo "IMChat 构建脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -h, --help      显示此帮助信息"
    echo "  -c, --clean     清理构建目录"
    echo "  -d, --debug     构建调试版本"
    echo "  -r, --release   构建发布版本（默认）"
    echo "  -t, --test      运行测试"
    echo "  -i, --install   安装到系统"
    echo "  -p, --package   创建安装包"
    echo "  --ninja         使用Ninja构建系统"
    echo ""
    echo "示例:"
    echo "  $0              # 构建发布版本"
    echo "  $0 -d           # 构建调试版本"
    echo "  $0 -c -r        # 清理后构建发布版本"
    echo "  $0 --ninja      # 使用Ninja构建"
}

# 检查依赖
check_dependencies() {
    print_info "检查构建依赖..."
    
    # 检查cmake
    if ! command -v cmake &> /dev/null; then
        print_error "cmake 未安装。请先安装cmake。"
        exit 1
    fi
    
    # 检查编译器
    if ! command -v g++ &> /dev/null && ! command -v clang++ &> /dev/null; then
        print_error "未找到C++编译器。请安装g++或clang++。"
        exit 1
    fi
    
    print_success "依赖检查通过"
}

# 清理构建目录
clean_build() {
    print_info "清理构建目录..."
    rm -rf build build-debug build-release build-ninja
    print_success "清理完成"
}

# 构建项目
build_project() {
    local preset=$1
    local build_type=$2
    
    print_info "使用预设 '$preset' 构建项目..."
    
    # 配置项目
    cmake --preset $preset
    
    # 构建项目
    cmake --build --preset $preset
    
    print_success "构建完成！"
    print_info "可执行文件位于: build*/bin/"
}

# 安装项目
install_project() {
    print_info "安装项目到系统..."
    
    if [ ! -d "build" ]; then
        print_error "请先构建项目"
        exit 1
    fi
    
    cd build
    sudo make install
    cd ..
    
    print_success "安装完成"
}

# 创建安装包
create_package() {
    print_info "创建安装包..."
    
    if [ ! -d "build" ]; then
        print_error "请先构建项目"
        exit 1
    fi
    
    cd build
    cpack
    cd ..
    
    print_success "安装包创建完成"
}

# 运行服务器
run_server() {
    print_info "启动聊天服务器..."
    
    if [ -f "build/bin/server" ]; then
        ./build/bin/server
    elif [ -f "build-release/bin/server" ]; then
        ./build-release/bin/server
    elif [ -f "build-debug/bin/server" ]; then
        ./build-debug/bin/server
    else
        print_error "未找到服务器可执行文件，请先构建项目"
        exit 1
    fi
}

# 运行客户端
run_client() {
    print_info "启动聊天客户端..."
    
    if [ -f "build/bin/client" ]; then
        ./build/bin/client
    elif [ -f "build-release/bin/client" ]; then
        ./build-release/bin/client
    elif [ -f "build-debug/bin/client" ]; then
        ./build-debug/bin/client
    else
        print_error "未找到客户端可执行文件，请先构建项目"
        exit 1
    fi
}

# 主函数
main() {
    local clean_flag=false
    local build_type="release"
    local install_flag=false
    local package_flag=false
    local use_ninja=false
    local run_server_flag=false
    local run_client_flag=false
    
    # 解析命令行参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help)
                show_help
                exit 0
                ;;
            -c|--clean)
                clean_flag=true
                shift
                ;;
            -d|--debug)
                build_type="debug"
                shift
                ;;
            -r|--release)
                build_type="release"
                shift
                ;;
            -i|--install)
                install_flag=true
                shift
                ;;
            -p|--package)
                package_flag=true
                shift
                ;;
            --ninja)
                use_ninja=true
                shift
                ;;
            --run-server)
                run_server_flag=true
                shift
                ;;
            --run-client)
                run_client_flag=true
                shift
                ;;
            *)
                print_error "未知选项: $1"
                show_help
                exit 1
                ;;
        esac
    done
    
    # 检查依赖
    check_dependencies
    
    # 清理构建目录
    if [ "$clean_flag" = true ]; then
        clean_build
    fi
    
    # 选择构建预设
    local preset="default"
    if [ "$use_ninja" = true ]; then
        preset="ninja"
    else
        preset="$build_type"
    fi
    
    # 构建项目
    if [ "$run_server_flag" = false ] && [ "$run_client_flag" = false ]; then
        build_project $preset $build_type
    fi
    
    # 安装项目
    if [ "$install_flag" = true ]; then
        install_project
    fi
    
    # 创建安装包
    if [ "$package_flag" = true ]; then
        create_package
    fi
    
    # 运行服务器
    if [ "$run_server_flag" = true ]; then
        run_server
    fi
    
    # 运行客户端
    if [ "$run_client_flag" = true ]; then
        run_client
    fi
}

# 运行主函数
main "$@" 